#include "WInterrupts.h"
#include "Arduino.h"

#include "interrupt.h"

void attachInterrupt(uint32_t pin, callback_function_t callback, uint32_t mode)
{
    PinName p = digitalPinToPinName(pin);
    auto port = set_GPIO_Port_Clock(CH_PORT(p));
    if (!port)
        return;
    uint32_t it_mode;
    switch (mode)
    {
    case CHANGE:
        it_mode = EXTI_Trigger_Rising_Falling;
        break;
    case FALLING:
    case LOW:
        it_mode = EXTI_Trigger_Falling;
        break;
    case RISING:
    case HIGH:
        it_mode = EXTI_Trigger_Rising;
        break;
    default:
        it_mode = EXTI_Trigger_Rising;
        break;
    }

    adapte_interrupt_enable(port, REF_GPIO_PIN(p), callback, it_mode);
}

void attachInterrupt(uint32_t pin, void (*callback)(void), uint32_t mode)
{
    callback_function_t _c = callback;
    attachInterrupt(pin, _c, mode);
}

void detachInterrupt(uint32_t pin)
{
    PinName p = digitalPinToPinName(pin);
    GPIO_TypeDef *port = get_GPIO_Port(CH_PORT(p));

    adapte_interrupt_disable(port, get_GPIO_Pin(p));
}
